<template>
  <a-modal
    :title="formInfo.id==undefined?'新增':'修改'"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
            <a-row>
                <a-col :span="24">
                    <a-form-model-item label="编号" prop="no" :rules="$valid.required" :maxLength="50">
                        <a-input v-model="formInfo.no" placeholder="请输入编号"/>
                    </a-form-model-item>
                </a-col>
              <a-col :span="24">
            <a-form-model-item label="名称" prop="name" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.name" placeholder="请输入名称"/>
            </a-form-model-item>
          </a-col>
                  <a-col :span="24">
            <a-form-model-item label="资产" prop="equipId" :rules="$valid.required" :maxLength="50">
                <a-tree-select
                        show-search
                        tree-node-filter-prop="title"
                        multiline
                        v-model="formInfo.equipId"
                        style="width: 100%"
                        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                        :tree-data="equipTree"
                        placeholder="请选择"
                >
                </a-tree-select>
            </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>
              <a-col :span="24">
            <a-form-model-item label="非生项目" prop="repairId" :rules="$valid.required" :maxLength="50">
              <!--<a-input v-model="formInfo.repairId" placeholder="请输入维修项目id"/>-->
                <a-tree-select
                        show-search
                        tree-node-filter-prop="title"
                        v-model="formInfo.repairId"
                        style="width: 100%"
                        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                        :tree-data="repairTree"
                        placeholder="请选择"
                >
                </a-tree-select>
            </a-form-model-item>
          </a-col>
            </a-row>
                <a-row>
                  <a-col :span="24">
            <a-form-model-item label="维修内容" prop="content" :rules="$valid.required" :maxLength="50">
              <a-input v-model="formInfo.content" type="textarea" placeholder="请输入维修内容"/>
            </a-form-model-item>
          </a-col>
            </a-row>

                <a-row>
            <a-col :span="24">
                <a-form-model-item   label="备注">
                    <a-input v-model="formInfo.remark" type="textarea"/>
                </a-form-model-item>
            </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  export default {
    name: 'EquRepairDeailEdit',
    components: {
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        formInfo: {
          name: undefined,
          equipId: undefined,
          repairId: undefined,
          no: undefined,
          addressId: undefined,
          buildingId: undefined,
          floorId: undefined,
          roomId: undefined,
          address: undefined,
          remark: undefined,
          content: undefined,
          departCode: undefined,
          createTime: undefined,
          sortOrder: undefined,
          wx: ''
        },
          equipTree:[],
          repairTree:[],
        url: {
          saveInfo: '/system/equRepairDeail/save',
          getequipTree:'/system/equEquipment/tree',
            getRepaireTree:'/system/equRepair/tree',
          getInfo: '/system/equRepairDeail/info'
        }
      }
    },
    created () {
        this.getTypeListTree()
        this.getEquRepqireTree()
    },
    methods: {
      add (equipid,reapirid) {
          console.log(equipid,reapirid)
          if(equipid){
              this.$set(this.formInfo,"equipId",equipid)
          }
          if(reapirid){
              this.$set(this.formInfo,"repairId",reapirid)
          }
        this.edit()
      },
      edit (record) {
        this.$objToOverride(this.formInfo, record)
        if (this.formInfo.id) {
          getAction(this.url.getInfo, { 'id': record.id }).then(res => {
            this.$nextTick(() => {
              this.formInfo = res.result.data
            })
          })
        }
        this.visible = true
      },
        getTypeListTree() {
            getAction(this.url.getequipTree).then((res) => {
                this.equipTree = res.result.data
            })
        },
        getEquRepqireTree(){
            getAction(this.url.getRepaireTree).then((res) => {
                this.repairTree = res.result.data
            })
        },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
          if (valid) {
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      // 关闭
      handleCancel () {
        this.close()
      },
      close () {
        this.$refs.ruleForm.resetFields()
        this.$objToReset(this.formInfo)
          this.roomValue=[]
        this.$emit('close')
        this.visible = false
      },
      editorOk (val) {
        this.formInfo.content = val
      },
        filterTreeNodes(inputValue, treeNode)  {
          console.log(inputValue,treeNode)
            return treeNode.title.indexOf(inputValue) !== -1; // 根据 label 进行过滤
        },
        selectEquip(selectkeys){
          console.log(selectkeys)
          this.formInfo.equipId = selectkeys.key
        },
        // 值变化处理
        handleChange(value, selectedOptions) {
            console.log(value)
            this.formInfo.roomValue = value.join(",")
        }
    }
  }
</script>
